<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
// src/com/xxxx/controller/BlogServlet.java
=======
>>>>>>> 2c9a92f (新增删除博客功能)
=======
>>>>>>> a05f6f6 (密码密文存储)
=======
>>>>>>> 2acfcf7 (详情页)
=======
>>>>>>> d93a5bb (移除index页)
package com.xxxx.controller;

import com.xxxx.entity.Blog;
import com.xxxx.entity.User;
import com.xxxx.entity.vo.MessageModel;
import com.xxxx.service.BlogService;
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD

=======
>>>>>>> 2c9a92f (新增删除博客功能)
=======
>>>>>>> a05f6f6 (密码密文存储)
=======
>>>>>>> 2acfcf7 (详情页)
=======
>>>>>>> d93a5bb (移除index页)
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    private final BlogService blogService = new BlogService();

<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
=======
    // POST 处理创建和删除操作
>>>>>>> 2c9a92f (新增删除博客功能)
=======
    // POST 处理创建和删除操作
>>>>>>> a05f6f6 (密码密文存储)
=======
    // POST 处理创建和删除操作
>>>>>>> 2acfcf7 (详情页)
=======
    // POST 处理创建和删除操作
>>>>>>> d93a5bb (移除index页)
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect("login.jsp");
            return;
        }

<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
        String title = request.getParameter("title");
        String content = request.getParameter("content");

        Blog blog = new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        blog.setUserId(user.getUserid());

        MessageModel mm = blogService.createBlog(blog);
        if (mm.getCode() == 1) {
            response.sendRedirect("blogList.jsp");
        } else {
            request.getSession().setAttribute("msgModel", mm);
            response.sendRedirect("blogEdit.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String type = request.getParameter("type"); // 获取请求参数
        List<Blog> blogs;

        if ("all".equals(type)) {
            // 显示所有博客
            blogs = blogService.getAllBlogs();
        } else {
            // 默认显示当前用户的博客
            User user = (User) request.getSession().getAttribute("user");
            if (user == null) {
                response.sendRedirect("login.jsp");
                return;
            }
=======
=======
>>>>>>> a05f6f6 (密码密文存储)
=======
>>>>>>> 2acfcf7 (详情页)
=======
>>>>>>> d93a5bb (移除index页)
        // 判断操作类型
        String action = request.getParameter("action");
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        if ("delete".equalsIgnoreCase(action)) {
            // 处理删除请求
            try {
                int blogId = Integer.parseInt(request.getParameter("id"));
                System.out.println("收到请求：action ="+action+",id ="+blogId);

                boolean success = blogService.deleteBlog(blogId,user.getUserid());

                if (success) {
                    response.getWriter().write("{\"success\": true}");
                } else {
                    response.getWriter().write("{\"success\": false, \"message\": \"删除失败或无权限\"}");
                }
            } catch (NumberFormatException e) {
                response.getWriter().write("{\"success\": false, \"message\": \"参数错误\"}");
            }
        } else {
            // 原有创建博客逻辑
            String title = request.getParameter("title");
            String content = request.getParameter("content");

            Blog blog = new Blog();
            blog.setTitle(title);
            blog.setContent(content);
            blog.setUserId(user.getUserid());

            MessageModel mm = blogService.createBlog(blog);
            if (mm.getCode() == 1) {
                response.sendRedirect("blog");
            } else {
                request.getSession().setAttribute("msgModel", mm);
                response.sendRedirect("blogEdit.jsp");
            }
        }
    }

    // GET 处理博客列表展示
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String type = request.getParameter("type");
<<<<<<< HEAD
<<<<<<< HEAD
        List<Blog> blogs;
        User user = (User) request.getSession().getAttribute("user");

        // 未登录用户强制跳转登录
=======
=======
>>>>>>> d93a5bb (移除index页)
        User user = (User) request.getSession().getAttribute("user");

        // 处理详情页请求（需要优先处理）
        if ("detail".equals(type)) {
            handleDetailRequest(request, response);
            return;
        }

        // 未登录用户强制跳转登录（个人页面需要登录）
<<<<<<< HEAD
>>>>>>> 2acfcf7 (详情页)
=======
>>>>>>> d93a5bb (移除index页)
        if (user == null && !"all".equals(type)) {
            response.sendRedirect("login.jsp");
            return;
        }

<<<<<<< HEAD
<<<<<<< HEAD
        if ("all".equals(type)) {
            blogs = blogService.getAllBlogs();
        } else {
<<<<<<< HEAD
>>>>>>> 2c9a92f (新增删除博客功能)
=======
>>>>>>> a05f6f6 (密码密文存储)
=======
=======
>>>>>>> d93a5bb (移除index页)
        List<Blog> blogs;
        if ("all".equals(type)) {
            blogs = blogService.getAllBlogs();
        } else {
<<<<<<< HEAD
>>>>>>> 2acfcf7 (详情页)
=======
>>>>>>> d93a5bb (移除index页)
            blogs = blogService.getUserBlogs(user.getUserid());
        }

        request.setAttribute("blogs", blogs);
        request.getRequestDispatcher("blogList.jsp").forward(request, response);
    }
<<<<<<< HEAD
<<<<<<< HEAD
=======
=======
>>>>>>> d93a5bb (移除index页)

    private void handleDetailRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            int blogId = Integer.parseInt(request.getParameter("id"));
            Blog blog = blogService.getBlogById(blogId);
            if (blog != null) {
                request.setAttribute("blog", blog);
                request.getRequestDispatcher("blogdetail.jsp").forward(request, response);
                System.out.println(request.getParameter("blog"));
            } else {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, "留言不存在");
            }
        } catch (NumberFormatException e) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "无效的ID格式");
        }
    }
<<<<<<< HEAD
>>>>>>> 2acfcf7 (详情页)
=======
>>>>>>> d93a5bb (移除index页)
}